package com.china_goldcard.loadingdialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.TextView;

import com.pnikosis.materialishprogress.ProgressWheel;

/**
 * 加载对话框
 */
public class LoadingDialog extends Dialog {

    private View mDialogView;
    private AnimationSet mModalInAnim;
    private AnimationSet mModalOutAnim;
    private boolean mCloseFromCancel;
    private TextView mLoadingTv;
    private String mLoadingText = "加载中...";
    private ProgressWheel wheel;

    /**
     * 加载框构造函数
     * @param context
     * @param canCancel
     */
    public LoadingDialog(Context context, boolean canCancel){

        this(context);

        //默认返回键可以取消
        setCancelable(canCancel);

        //其他区域不可取消
        setCanceledOnTouchOutside(canCancel);
    }

    /**
     * 加载框构造函数
     * @param context
     */
    public LoadingDialog(Context context) {

        super(context, R.style.loading_dialog_style);

        // 弹框出现时的动画
        mModalInAnim = (AnimationSet) AnimationLoader.loadAnimation(getContext(), R.anim.dialog_in);

        // 弹框消失时的动画
        mModalOutAnim = (AnimationSet) AnimationLoader.loadAnimation(getContext(), R.anim.dialog_out);

        mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mCloseFromCancel) {
                            LoadingDialog.super.cancel();
                        } else {
                            LoadingDialog.super.dismiss();
                        }
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_dialog_view);
        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        mLoadingTv = (TextView) findViewById(R.id.loading_text);
        wheel = (ProgressWheel) findViewById(R.id.loading_dialog_progress);
        mLoadingTv.setText(mLoadingText);
    }

    @Override
    protected void onStart() {
        mDialogView.startAnimation(mModalInAnim);
    }

    @Override
    public void cancel() {
        dismissWithAnimation(true);
    }

    @Override
    public void show() {
        super.show();
        wheel.spin();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        wheel.stopSpinning();
    }

    /**
     * 弹框消失动画
     * @param fromCancel
     */
    private void dismissWithAnimation(boolean fromCancel) {
        mCloseFromCancel = fromCancel;
        mDialogView.startAnimation(mModalOutAnim);
    }


    /**
     * 设置加载中文字
     * @param loadingText
     */
    public void setLoadingText(String loadingText) {
        this.mLoadingText = loadingText;
    }
}
